package com.client.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.auth.model.Account;
import com.client.model.ICpUserData;
import com.client.model.IUserData;
import com.manage.config.Config;
import com.service.AbstractService;
import com.util.ConfigUtil;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

public class CpUserDataServiceImpl extends AbstractService implements CpUserDataService {
	private Logger logger;
	
	public CpUserDataServiceImpl()
	{
		logger = Logger.getLogger(this.getClass());
		this.dbcp = ConfigUtil.get(com.manage.config.Config.SECTION, Config.KEY_DBCP_NAME);
	}

	@Override
	public List<ICpUserData> getCpUserData(Account operator, String date1,
			String date2, int plugin, int channel, int project,
			String province, String region, String dimension) {
		String sql = "";
		{
			String gb = "";
			List<String> fl = new ArrayList<String>();
			
			if (!checkQueryDimension(dimension, "day"))
				gb += "0 ";
			else
				fl.add("day");
			gb += "day,";
			
			if (!checkQueryDimension(dimension, "hour"))
				gb += "0 ";
			else
				fl.add("hour");
			gb += "hour,";
			
			if (!checkQueryDimension(dimension, "minute"))
				gb += "0 ";
			else
				fl.add("minute");
			gb += "minute,";

			if (!checkQueryDimension(dimension, "plugin"))
				gb += "-1 ";
			else
				fl.add("plugin");
			gb += "plugin,";

			if (!checkQueryDimension(dimension, "channel"))
				gb += "-1 ";
			else
				fl.add("channel");
			gb += "channel,";

			if (!checkQueryDimension(dimension, "project"))
				gb += "-1 ";
			else
				fl.add("project");
			gb += "project,";

			if (!checkQueryDimension(dimension, "province"))
				gb += "'' ";
			else
				fl.add("province");
			gb += "province,";

			if (!checkQueryDimension(dimension, "region"))
				gb += "'' ";
			else
				fl.add("region");
			gb += "region,";
						
			sql = "select 0 id," + gb + "count,newcount " +
				"from (select " + fl.toString().replace("[", "").replace("]", "").replace(" ", "") + ",sum(count) count,sum(newcount) newcount   from cpuserdata " +
				"where (day>=" + date1.replace("-", "")  + " " +
				"and day<=" + date2.replace("-", "") + ") ";
			if (channel > -1)
				sql += " and channel=" + channel + " ";
			else
				sql += " and channel in (select channelid from accountchannel where accountid='" + operator.getId() + "') ";
			if (project > -1)
				sql += " and project=" + project + " ";
			if (province != null && !province.equalsIgnoreCase("ALL"))
				sql += "and province='" + province +"' ";
			if (region != null && !region.equalsIgnoreCase("ALL"))
				sql += "and region='" + region + "' ";
			sql += "group by " + fl.toString().replace("[", "").replace("]", "").replace(" ", "") + ") a " +
					"order by day,hour,minute,plugin,channel,project,province,region";
		}
		logger.debug("sql:"+sql);
		if (sql.length() > 0) {
			ModelGenerator<ICpUserData> mg = new ModelGenerator<ICpUserData>(
					ICpUserData.class);
			return mg.generateList(dbcp, sql);
		}
		return null;
	}
	@Override
	public boolean addCpUserData( ICpUserData data) {
		data.setId(getTableId("cpuserdata"));
		String sql = 
				"insert into cpuserdata(id,day,hour,minute,plugin,channel,project,province,region,count,newcount) " +
				"values(" + data.getId() + "," + data.getDay() + "," + data.getHour() + "," +
						data.getMinute() + "," + data.getPlugin() + "," + data.getChannel() + "," + 
						data.getProject() + ",'" + data.getProvince() + "','" + data.getRegion() + "',"+data.getCount()+","+data.getNewcount()+")";
		return ConnectionManager.getInstance().execute(dbcp, sql);
	}
	@Override
	public boolean updateCpUserData(ICpUserData data) {
		String sql = 
				"update cpuserdata set count="+data.getCount()+", newcount="+data.getNewcount() +
				" where day=" + data.getDay() + " and hour=" + data.getHour() + 
				" and minute=" + data.getMinute() + " and plugin=" + data.getPlugin() + 
				" and channel=" + data.getChannel() + " and project=" + data.getProject() + 
				" and province='" + data.getProvince() + "' and region='" + data.getRegion() + "'";
		return ConnectionManager.getInstance().execute(dbcp, sql);
	}
}
